package net.zjitc.controller;

import net.zjitc.pojo.Book;
import net.zjitc.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request) {
        request.setAttribute("books", bookService.findAll());
        return "list.jsp";
    }

    @RequestMapping("/save")
    public String save(Book book) {
        if(bookService.save(book)) {
            System.out.println("save success");
        }else {
            System.out.println("save failed");
        }
        return "redirect:/findAll";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        if(bookService.delete(id)) {
            System.out.println("delete success");
        }else {
            System.out.println("delete failed");
        }
        return "redirect:/findAll";
    }

    @RequestMapping("/update")
    public String update(Book book) {
        if(bookService.update(book)) {
            System.out.println("update success");
        }else {
            System.out.println("update failed");
        }
        return "redirect:/findAll";
    }

    @RequestMapping("/findById/{id}")
    public String findById(@PathVariable Integer id,HttpServletRequest request) {
        request.setAttribute("books", bookService.findById(id));
        return "/update.jsp";
    }
}